
#include "osgGA/GUIEventAdapter"
#include "osgMeasureAreaEvent.h"
#include "Windows.h"
#include "publicDefined.h"
#include "IUserEventCallBack.h"

COsgMeasureAreaEvent::COsgMeasureAreaEvent(osgViewer::View *view, osg::Group *root, IEventCall *iCall)
    : CBaseUserEvent(view),
      mView(view),
      mRoot(root),
      miCall(iCall),
      isActivated(false)
{
    mEditSceneGroup = mRoot->getChild(1)->asGroup();
}

bool COsgMeasureAreaEvent::handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa)
{
    if (isActivated)
    {
        if (IsLeftClick(ea) && GetKeyState(VK_CONTROL) < 0)
        {
            //碰撞
            CPickResult pickResult = PickResult(ea.getX(), ea.getY(), mView);
            osg::Vec3d worldPt = pickResult.worldPt;

            if (worldPt.x() == 0 && worldPt.y() == 0 && worldPt.z() == 0)
            {
                return false;
            }

            vecWorldCoord.push_back(worldPt);

            //计算面积
            double area = CalculateArea(vecWorldCoord);

            //发送消息 !
            char strMsg[MAX_PATH];
            sprintf_s(strMsg, "面积%lf", area);
            miCall->notifyMeasureMsg(strMsg);
        }
        else if (IsMouseMove(ea) && GetKeyState(VK_CONTROL) < 0)
        {
            if (vecWorldCoord.empty())
            {
                return false;
            }

             //碰撞
            CPickResult pickResult = PickResult(ea.getX(), ea.getY(), mView);
            osg::Vec3d worldPt = pickResult.worldPt;

            if (worldPt.x() == 0 && worldPt.y() == 0 && worldPt.z() == 0)
            {
                return false;
            }

            //计算面积
            std::vector<osg::Vec3d> vecTempCoord = vecWorldCoord;
            vecTempCoord.push_back(worldPt);
            double area = CalculateArea(vecTempCoord);

            //发送消息 !
            char strMsg[MAX_PATH];
            sprintf_s(strMsg, "面积%lf", area);
            miCall->notifyMeasureMsg(strMsg);
        }
        else if (IsDoubleClick(ea))
        {
             //碰撞
            CPickResult pickResult = PickResult(ea.getX(), ea.getY(), mView);
            osg::Vec3d worldPt = pickResult.worldPt;

            if (worldPt.x() == 0 && worldPt.y() == 0 && worldPt.z() == 0)
            {
                return false;
            }
            vecWorldCoord.push_back(worldPt);
            //计算面积
            double area = CalculateArea(vecWorldCoord);

            //发送消息 !
            char strMsg[MAX_PATH];
            sprintf_s(strMsg, "面积%lf", area);
            miCall->notifyMeasureMsg(strMsg);
        }
        else if(IsKeyPress(ea))
        {
            if (ea.getKey() == osgGA::GUIEventAdapter::KEY_BackSpace)
            {
                vecWorldCoord.pop_back();
                //消息置空
                char strMsg[99] = "";
            }
            else if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Z)					 //z
            {
                vecWorldCoord.clear();

                //消息置空
                char strMsg[99] = "";
            }
        }
    }

    return false;
}


void COsgMeasureAreaEvent::updateActivatedStatus(std::vector<OperatingState> vecOpState)
{
    bool flag = false;

    if (isActivated == true)
    {
        flag = true;
    }

    isActivated = isActivate(vecOpState, OPS_MEASURE_AREA);

     //刚不激活
    if (isActivated == false && flag == true)
    {
    }

    //刚激活
    if (isActivated && flag == false)
    {
    }
}
